package demo1;

public class MySingleList {

    public ListNote head;

    static class ListNote {
        public int data;
        public ListNote next;
        public ListNote(int data) {
            this.data = data;
        }
    }

    //头插法
    public void addFirst(int data){
        ListNote newNote = new ListNote(data);
        newNote.next = head;
        head = newNote;
    }
    //尾插法
    public void addLast(int data){
        ListNote cur = head;
        if(cur == null) {
            head = new ListNote(data);
            return;
        }
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = new ListNote(data);
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        ListNote cur = head;
        ListNote prev = head;
        if(index < 0 || index > size()) {
            System.out.println("没有那么多数据，index太大");

            return;
        }
        for (int i = 0; i < index; i++) {
            prev = cur;
            cur = cur.next;
        }

        if(head == cur) {
            addFirst(data);
            return;
        }
        ListNote newNote = new ListNote(data);
        newNote.next = cur;
        prev.next = newNote;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        ListNote cur = this.head;
        while(cur != null) {
            if(cur.data == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(head == null) {
            return;
        }
        if(head.data == key) {
            head = head.next;
            return;
        }
        ListNote prev = head;
        while(prev.next != null && prev.next.data != key) {
            prev = prev.next;
        }
        if(prev.next == null) {//说明只有一个但是不是要被删除的数值
            System.out.println("未找到");
        } else {
            prev.next = prev.next.next;
        }

    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(head == null) {
            return;
        }
        while(head.data == key) {
            head = head.next;
        }
//        ListNote cur = head;
        ListNote prev = head;
//        while(cur != null) {
//            if(cur.data == key) {
//                prev.next = cur.next;
//                cur = prev.next;
//                continue;
//            }
//            prev = cur;
//            cur = cur.next;
//        }
        while(prev.next != null) {
            if(prev.next.data == key) {
                prev.next = prev.next.next;
                continue;
            }
            prev = prev.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        ListNote cur = head;
        while(cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }
    //清除链表
    public void clear() {
        ListNote cur = head;
        while(cur != null) {
            ListNote del = cur;
            cur = cur.next;
            del.data = 0;//如果是引用类型，那么就置为 null
            del = null;
        }
        head = null;
    }
    //打印链表
    public void display() {
        ListNote cur = head;
        while(cur != null) {
            System.out.printf("%d->", cur.data);
            cur = cur.next;
        }
        System.out.println("null");
    }
}
